package com.spring;

import com.bean.Book;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;


@Configuration
@ComponentScan(basePackages = {"com.bean"})
public class ConfigurationTest {
    @Bean(name = "book")
    @Scope("prototype")
    public Book book() {
        return new Book();
    }

    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }

    @Bean(name = "dataSource")
    @Scope("prototype")
    public DataSource createDataSource() {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass("com.mysql.cj.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/db_test?useSSL=false&allowPublicKeyRetrieval=true");
            ds.setUser("root");
            ds.setPassword("1234");
            return ds;
        } catch (PropertyVetoException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
